//
//  LSTPopViewrRSidebarView.m
//  LSTPopView_Example
//
//  Created by LoSenTrad on 2020/4/24.
//  Copyright © 2020 490790096@qq.com. All rights reserved.
//

#import "LWFuncView.h"
#import "UIColor+LSTColor.h"
#import "UIView+LSTView.h"
#import "CCDTableViewCell.h"
#import "ZGDeviceInfo.h"
#import "LWSwithCell.h"
#import "UIColor+HEX.h"
#import "Masonry.h"
#import "CDModelClick.h"
#import "CDModelView.h"
#import "MJEncryptStringData.h"
#import "SCLAlertView.h"

@interface LWFuncView () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableDictionary *cellDic;

/** <#.....#> */
@property (nonatomic,strong) UIButton *btn1;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn2;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn3;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn4;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn5;
/** <#.....#> */
@property (nonatomic,strong) UIButton *btn6;


/* 视图 */
@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,  strong) UIVisualEffectView *blurView;


@property (nonatomic,strong) UIView *headerView;

@property (nonatomic,strong) UILabel *lable2;

@property (nonatomic,strong) UILabel *lable3;

@property (nonatomic,strong) UILabel *lable4;

@property (nonatomic,strong) UILabel *lable5;


@end

@implementation LWFuncView

- (NSMutableDictionary *)cellDic {
    
    if (_cellDic == nil) {
        
        _cellDic = [NSMutableDictionary dictionary];
    }
    
    return _cellDic;;
}

#pragma mark - ***** 初始化 *****

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        self.layer.cornerRadius = 7;
        self.layer.masksToBounds = YES;
        self.backgroundColor = [UIColor colorWithHex:0x000000 andAlpha:1];
        [self initSubViews:frame];
        
        // 监听网络变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iphoneNetworkStatusChangedClick) name:@"iphoneNetworkStatusChanged" object:nil];
        
    }
    return self;
}



#pragma mark - ***** setter 设置器/数据处理 *****

- (UITableView *)tableView {
    
    if (_tableView == nil) {
        
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedRowHeight = 44;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        
        _tableView.rowHeight = UITableViewCellSeparatorStyleNone;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        //这行代码必须加上，可以去除tableView的多余的线，否则会影响美观
        //        _tableView.tableFooterView = [UIView new];
        ///设置table没有分割线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        
        [_tableView registerClass:[LWSwithCell class] forCellReuseIdentifier:@"LWSwithCell"];
        
    }
    
    return _tableView;
}

#pragma mark - ***** setupUI 界面布局 *****

- (void)initSubViews:(CGRect)frame {
    
    self.blurView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    
    self.blurView.frame = frame;
    [self addSubview:self.blurView];
    
    [self.blurView.contentView addSubview:self.tableView];
    
    self.headerView = [UIView new];
    
    if ([CDModelClick currentScreenInfo].isS) {

        
        self.headerView.frame = CGRectMake(0, 0, frame.size.width, 50);
        
    } else {
        
        self.headerView.frame = CGRectMake(0, 0, frame.size.width, 30);
        
    }
    
    
    
    self.tableView.tableHeaderView = self.headerView;
    
    self.lable5 = [UILabel new];
    self.lable5.textColor = [UIColor whiteColor];
    self.lable5.font = [UIFont boldSystemFontOfSize:14];
    self.lable5.textAlignment = 1;
    self.lable5.text = mj_OCString(_1970084037);
    [self.headerView addSubview:self.lable5];
    
    [self.lable5 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.offset(0);
        make.top.offset(5);
        
    }];
    
    self.lable4 = [UILabel new];
    self.lable4.textColor = [UIColor whiteColor];
    self.lable4.font = [UIFont boldSystemFontOfSize:11];
    self.lable4.textAlignment = 1;
    self.lable4.hidden = YES;
    self.lable4.text = mj_OCString(_162568979);

    [self.headerView addSubview:self.lable4];
    
    [self.lable4 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.offset(0);
        make.top.offset(30);
        
    }];
    
    if ([CDModelClick currentScreenInfo].isS) {
        
        self.lable4.hidden = NO;
    }
    
    //    self.lable4 = [UILabel new];
    //    self.lable4.textColor = [UIColor whiteColor];
    //    self.lable4.font = [UIFont boldSystemFontOfSize:13];
    //    self.lable4.textAlignment = 1;
    //    self.lable4.text = @"我是一个简介";
    //
    //    [self.headerView addSubview:self.lable4];
    //    [self.lable4 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.centerX.offset(0);
    //        make.top.offset(40);
    //
    //    }];
    //
    //
    //    self.lable3 = [UILabel new];
    //    self.lable3.textColor = [UIColor whiteColor];
    //    self.lable3.font = [UIFont boldSystemFontOfSize:13];
    //    self.lable3.textAlignment = 1;
    //    self.lable3.text = [NSString stringWithFormat:@"%@ iOS %@",[ZGDeviceInfo currentDeviceInfo].getCurrentDevicePhoneType,[ZGDeviceInfo currentDeviceInfo].getSystemVersion];
    //
    //    [self.headerView addSubview:self.lable3];
    //    [self.lable3 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.centerX.offset(0);
    //        make.top.offset(66);
    //
    //    }];
    //
    //    self.lable2 = [UILabel new];
    //    self.lable2.textColor = [UIColor whiteColor];
    //    self.lable2.font = [UIFont boldSystemFontOfSize:11];
    //    self.lable2.textAlignment = 1;
    //    self.lable2.text = [NSString stringWithFormat:@"当前时间: %@",[UKCommonTools getCurrentTimes]];
    //
    //    [self.headerView addSubview:self.lable2];
    //    [self.lable2 mas_makeConstraints:^(MASConstraintMaker *make) {
    //
    //        make.centerX.offset(0);
    //        make.top.offset(90);
    //
    //    }];
    
    
    
    
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    ////            self.tableView.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn2.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn3.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn4.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn5.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    //
    //
    //    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //        [UIView animateWithDuration:0.25 animations:^{
    //            self.btn6.x = 20;
    //        } completion:^(BOOL finished) {
    //
    //        }];
    //    });
    
    
    
}

#pragma mark tableDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.titleArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *identifier = [self.cellDic objectForKey:[NSString stringWithFormat:@"LWSwithCell%@",indexPath]];
    // 如果取出的唯一标示符不存在，则初始化唯一标示符，并将其存入字典中，对应唯一标示符注册Cell
    if (identifier == nil) {
        identifier = [NSString stringWithFormat:@"%@%@", @"LWSwithCell", [NSString stringWithFormat:@"%@", indexPath]];
        [self.cellDic setValue:identifier forKey:[NSString stringWithFormat:@"%@", indexPath]];
        // 注册Cell
        [self.tableView registerClass:[LWSwithCell class] forCellReuseIdentifier:identifier];
    }
    
    URLModel *model = self.titleArr[indexPath.row];
    
    LWSwithCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    cell.timeLab.text = model.urltime;
    
    cell.titleLab.text = [NSString stringWithFormat:@"%ld. %@",(long)indexPath.row,model.urlData];
    
    cell.typeState.tag = indexPath.row;
    
    cell.tiAccBtn.hidden = YES;
    
    
    if ([model.urltype isEqualToString:@"0"]) {
        
        cell.typeState.selected = NO;
        
    } else {
        
        cell.typeState.selected = YES;
    }
    
    [cell.typeState addTarget:self action:@selector(titleSwitchClck:) forControlEvents:UIControlEventTouchUpInside];
    
  
  
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return 40;
}

- (void)setTitleArr:(NSArray *)titleArr {
    
    _titleArr = titleArr;
   
        for (int i =0; i< titleArr.count; i++) {
            
            URLModel *model = titleArr[i];
            
            model.urltype = @"0";
            
            for (int j =0; j< [ZGDeviceInfo currentDeviceInfo].installArrM.count; j++) {
                
            URLModel  *obj = [ZGDeviceInfo currentDeviceInfo].installArrM[j];
                
                if ([model.urlData isEqualToString:obj.urlData]) {
                    
                    model.urltype = @"1";
                    
                } else {
                    
                }
                
            }
            
            
        }
   
    [self.tableView reloadData];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
}


#pragma mark - 拦截

- (void)titleSwitchClck:(UIButton *)sw {
    
    URLModel *model = self.titleArr[sw.tag];
  
        if (sw.selected) {
            
            sw.selected = NO;
            
            model.urltype = @"0";
            
//            [URLModel deleteUserInfoWith:model];
            
            [[ZGDeviceInfo currentDeviceInfo].installArrM removeObject:model];
            
        } else {
            
            sw.selected = YES;
            
            model.urltype = @"1";
//            [model saveUserInfo];
            
            [[ZGDeviceInfo currentDeviceInfo].installArrM addObject:model];
        }
  
    [self.tableView reloadData];
    
    NSLog(@"当前tag=%ld",(long)sw.tag);
    
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    
}

- (void)iphoneNetworkStatusChangedClick {
    
    self.titleArr = [ZGDeviceInfo currentDeviceInfo].selectedArrM.mutableCopy;

    [self.tableView reloadData];
}

@end
